<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品列表浏览</title>

    <link rel="stylesheet" href="../static/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/global/css/components.css" type="text/css">
    <link rel="stylesheet" href="../static/assets/admin/pages/css/login.css" type="text/css">
    <script src="../static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
    <style>
        .list {
            background-color: #0093E9;
            background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
        }

    </style>

</head>

<body class="list">
<div class="content">
    <h3 class="form-title">商品列表浏览</h3>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>商品名</th>
                    <th>商品图片</th>
                    <th>商品描述</th>
                    <th>商品价格</th>
                    <th>商品库存</th>
                    <th>商品销量</th>
                </tr>
            </thead>
            <tbody id="container">

            </tbody>
        </table>
    </div>

</div>


<script>
    // 定义全局商品数组信息
    let g_itemList = [];
    $(function () {

            $.ajax({
                type: "GET",
                url: "http://localhost:8090/item/list",

                xhrFields:{withCredentials : true},
                success: function (data) {
                    if (data.status === "success") {
                        g_itemList = data.data;
                        reloadDom();
                    } else {
                        alert("获取商品信息失败，因为" + data.data.errMsg);
                    }
                },
                error: function (data) {
                    alert("获取商品信息失败，因为" + data.responseText);
                }
            })
    });

    function reloadDom() {
        for (let i = 0; i < g_itemList.length; i++) {
            let itemVo = g_itemList[i];
            let dom = "<tr data-id='"+ itemVo.id +"' id='itemDetail"+ itemVo.id +"'>" +
                "<td>"+ itemVo.title +"</td>" +
                "<td><img style='width:100px; height:auto;' src='"+ itemVo.imgUrl +"'/></td>" +
                "<td>"+ itemVo.description +"</td>" +
                "<td>"+ itemVo.price +"</td>" +
                "<td>"+ itemVo.stock +"</td>" +
                "<td>"+ itemVo.sales +"</td>" +
                "</tr>";
            $("#container").append($(dom));

            $("#itemDetail" + itemVo.id).click(function () {
                window.location.href = "getItem.html?id=" + $(this).data("id");
            });
        }
    }
</script>

</body>
</html>